//
//  HTClassHistoryVC.swift
//  QuitSmokingApp
//  
//  Created by wealon on 2024.
//  QuitSmokingApp.
//  
    

import UIKit
import SnapKit

class HTClassHistoryVC: HTClassViewController {
    let var_item_width = (HTClassTool.STATIC_screenWidth - 14.auto()*2)/4
    let var_item_height = 48.auto()
    lazy var var_titleLabel: UILabel = {
        let var_label = UILabel()
        var_label.text = HTClassTool.ht_obtainYearMonth()
        var_label.textColor = .white
        var_label.font = .systemFont(ofSize: 28, weight: .bold)
        return var_label
    }()
    lazy var var_date_select: UIButton = {
        let var_button = UIButton()
        var_button.onTap { [weak self] in
            HTClassYearMonthTool.ht_showDatePick {[weak self] var_date in
                self?.var_titleLabel.text = HTClassTool.ht_convertToFixedFormat(HTClassTool.ht_convertToFixedFormat(var_date))
                self?.var_current_date = HTClassTool.ht_convertToFixedFormat(HTClassTool.ht_convertToFixedFormat(var_date))
                self?.var_dataSource = HTClassCalculationTool.ht_getPlanDataValue(HTClassTool.ht_convertToFixedFormat(var_date))
                self?.var_tableView.reloadData()
            }
        }
        var_button.setImage(UIImage(named: "icon_date_select_icon"), for: .normal)
        return var_button
    }()
    var var_current_date: String = HTClassTool.ht_obtainYearMonth()
    lazy var var_zoneHeadView: UIView = {
        let var_view = UIView(frame: CGRect(x: 14.auto(), y: 0, width: HTClassTool.STATIC_screenWidth - 14.auto()*2, height: 48.auto()))
        var_view.backgroundColor = UIColor("#212329")
        let var_dateLabel = ht_createLabel("DATE")
        var_view.addSubview(var_dateLabel)
        var_dateLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        let var_date_line = UILabel()
        var_date_line.backgroundColor = UIColor("#525252")
        var_view.addSubview(var_date_line)
        var_date_line.snp.makeConstraints { make in
            make.left.equalTo(var_dateLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview().offset(15.auto())
        }
        // =======
        let var_numberLabel = ht_createLabel("NUMBER")
        var_view.addSubview(var_numberLabel)
        var_numberLabel.snp.makeConstraints { make in
            make.left.equalTo(var_dateLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        let var_number_line = UILabel()
        var_number_line.backgroundColor = UIColor("#525252")
        var_view.addSubview(var_number_line)
        var_number_line.snp.makeConstraints { make in
            make.left.equalTo(var_numberLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview().offset(15.auto())
        }
        // =======
        let var_costLabel = ht_createLabel("COST")
        var_view.addSubview(var_costLabel)
        var_costLabel.snp.makeConstraints { make in
            make.left.equalTo(var_numberLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        let var_cost_line = UILabel()
        var_cost_line.backgroundColor = UIColor("#525252")
        var_view.addSubview(var_cost_line)
        var_cost_line.snp.makeConstraints { make in
            make.left.equalTo(var_costLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview().offset(15.auto())
        }
        // =======
        let var_tarLabel = ht_createLabel("TA'R")
        var_view.addSubview(var_tarLabel)
        var_tarLabel.snp.makeConstraints { make in
            make.left.equalTo(var_costLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        var_view.ht_roundCorners([.topLeft, .topRight], var_radius: 8)
        return var_view
    }()
    lazy var var_tableView: UITableView = {
        let tabelView = UITableView(frame: .zero, style: .plain)
        tabelView.backgroundColor = .clear
        tabelView.delegate = self
        tabelView.dataSource = self
        tabelView.tableFooterView = UIView()
        tabelView.estimatedRowHeight = 0
        tabelView.estimatedSectionFooterHeight = 0
        tabelView.estimatedSectionHeaderHeight = 0
        tabelView.register(HTClassHistoryCell.self, forCellReuseIdentifier: "HTClassHistoryCell")
        tabelView.sectionHeaderTopPadding = 0
        tabelView.layer.cornerRadius = 8.auto()
        tabelView.layer.masksToBounds = true
        tabelView.showsVerticalScrollIndicator = false
        return tabelView
    }()
    var var_dataSource:  [SmokingCessationPlanModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.var_dataSource = HTClassCalculationTool.ht_getPlanDataValue(HTClassTool.ht_obtainYearMonth())
        self.var_tableView.reloadData()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        ht_initNav()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        var_titleLabel.removeFromSuperview()
        var_date_select.removeFromSuperview()
    }
    func ht_createLabel(_ var_title: String) -> UILabel {
        let var_label = UILabel()
        var_label.text = var_title
        var_label.textColor = .white
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .bold)
        return var_label
    }
    override func ht_setUI() {
        super.ht_setUI()
        
        
        self.view.addSubview(var_tableView)
        var_tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(14.auto())
            make.right.equalToSuperview().offset(-14.auto())
            make.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(HTClassTool.STATIC_navigationBarTotalHeight + 80.auto())
        }
    }
    
    func ht_initNav() {
        self.navigationController?.navigationBar.addSubview(var_titleLabel)
        var_titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(-10.auto())
            make.centerY.equalToSuperview()
        }
        self.navigationController?.navigationBar.addSubview(var_date_select)
        var_date_select.snp.makeConstraints { make in
            make.centerY.equalTo(var_titleLabel.snp.centerY)
            make.left.equalTo(var_titleLabel.snp.right).offset(6)
        }
    }
}


extension HTClassHistoryVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return var_dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let var_cell = tableView.dequeueReusableCell(withIdentifier: "HTClassHistoryCell", for: indexPath) as! HTClassHistoryCell
        let var_model = self.var_dataSource[indexPath.row]
        var_cell.var_dateLabel.text = "\(HTClassTool.ht_getDay(var_model.var_date) ?? 0) st"
        var_cell.var_numberLabel.text = var_model.var_cigarettes_smoked_today
        let var_price = Double(var_model.var_cigarettePrice) ?? 0.0
        let var_number = Double(var_model.var_cigarettes_smoked_today) ?? 0.0
        let var_tar = Double(var_model.var_tarContentPerCigarette) ?? 0.0
        var_cell.var_costLabel.text = String(format: "%.2f", var_price * var_number)
        var_cell.var_tarLabel.text = String(format: "%.2f", var_tar * var_number)
        return var_cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 48.auto()
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return self.var_zoneHeadView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 48.auto()
    }
}

class HTClassHistoryCell: UITableViewCell {
    lazy var var_dateLabel: UILabel = {
        let var_label = UILabel()
        var_label.textColor = .white
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .bold)
        return var_label
    }()
    lazy var var_numberLabel: UILabel = {
        let var_label = UILabel()
        var_label.textColor = .white
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .bold)
        return var_label
    }()
    lazy var var_costLabel: UILabel = {
        let var_label = UILabel()
        var_label.textColor = .white
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .bold)
        return var_label
    }()
    lazy var var_tarLabel: UILabel = {
        let var_label = UILabel()
        var_label.textColor = .white
        var_label.textAlignment = .center
        var_label.font = .systemFont(ofSize: 18, weight: .bold)
        return var_label
    }()
    lazy var var_date_line: UILabel = {
        let var_label = UILabel()
        var_label.backgroundColor = UIColor("#525252")
        return var_label
    }()
    lazy var var_number_line: UILabel = {
        let var_label = UILabel()
        var_label.backgroundColor = UIColor("#525252")
        return var_label
    }()
    lazy var var_cost_line: UILabel = {
        let var_label = UILabel()
        var_label.backgroundColor = UIColor("#525252")
        return var_label
    }()
    let var_item_width = (HTClassTool.STATIC_screenWidth - 14.auto()*2)/4
    let var_item_height = 48.auto()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        ht_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ht_setUI() {
        self.contentView.backgroundColor = UIColor("#212329")
        self.contentView.addSubview(var_dateLabel)
        self.contentView.addSubview(var_numberLabel)
        self.contentView.addSubview(var_costLabel)
        self.contentView.addSubview(var_tarLabel)
        self.contentView.addSubview(var_date_line)
        self.contentView.addSubview(var_number_line)
        self.contentView.addSubview(var_cost_line)
        
        var_dateLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }

        var_date_line.snp.makeConstraints { make in
            make.left.equalTo(var_dateLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview()
        }
        // =======
        var_numberLabel.snp.makeConstraints { make in
            make.left.equalTo(var_dateLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        var_number_line.snp.makeConstraints { make in
            make.left.equalTo(var_numberLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview()
        }
        // =======
        var_costLabel.snp.makeConstraints { make in
            make.left.equalTo(var_numberLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        var_cost_line.snp.makeConstraints { make in
            make.left.equalTo(var_costLabel.snp.right)
            make.bottom.equalToSuperview()
            make.width.equalTo(1)
            make.top.equalToSuperview()
        }
        // =======
        var_tarLabel.snp.makeConstraints { make in
            make.left.equalTo(var_costLabel.snp.right)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(var_item_width)
        }
        
        let var_top_lin = UILabel()
        var_top_lin.backgroundColor = UIColor("#525252")
        self.contentView.addSubview(var_top_lin)
        var_top_lin.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(1)
            make.left.equalToSuperview().offset(10.auto())
            make.right.equalToSuperview().offset(-10.auto())
        }
    }
}
